﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Quartz;
using TaskManagement.Context;
using TaskManagement.Jobs;
using TaskManagement.Model;
using TaskManagement.Service;
using TaskManagement.Utils;

namespace TaskManagement.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TaskController : ControllerBase
    {
        //public static List<int> taskIds = new List<int>();

        /// <summary>
        /// 从数据库获取所有任务
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult<List<TASK>> GetAllTask()
        {
            var db = new DbFactory().GetDbContext();
            var tasks = db.Queryable<TASK>().ToList();
            return tasks;
        }

        /// <summary>
        /// 添加任务到数据库
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<List<TASK>> AddTask(TASK task)
        {
            var db = new DbFactory().GetDbContext();
            db.Insertable(task).ExecuteCommand();
            var tasks = db.Queryable<TASK>().ToList();
            return tasks;
        }


        /// <summary>
        /// 执行任务
        /// </summary>
        /// <param name="taskId"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<List<int>> startTask(int taskId)
        {
            var db = new DbFactory().GetDbContext();
            var task = db.Queryable<TASK>().Where(p => p.ID == taskId).First();
            if (TaskService.taskIds.Contains(taskId))
            {
                return TaskService.taskIds;
            }
            if (task.TYPE == "ping")
            {
                var pingJob = QuartzHelpers.StartAsync<PingJob>(new TimeSpan(0, 0, task.TIMESPAN), "job_" + task.ID, "trigger_" + task.ID, "pingGroup", task.NAME);
                TaskService.taskIds.Add(taskId);
            }
            else if (task.TYPE == "telnet")
            {
                var telnetJob = QuartzHelpers.StartAsync<TelnetJob>(new TimeSpan(0, 0, task.TIMESPAN), "job_" + task.ID, "trigger_" + task.ID, "telnetGroup", task.NAME);
                TaskService.taskIds.Add(taskId);
            }


            return TaskService.taskIds;
        }


        [HttpPost]
        public int deleteTaskFromScheduler(int taskId)
        {
            var db = new DbFactory().GetDbContext();
            var task = db.Queryable<TASK>().Where(p => p.ID == taskId).First();
            if (task.TYPE == "ping")
            {
                var jobKey = new JobKey("job_" + task.ID, "pingGroup");
                TaskService.taskIds.Remove(taskId);
                QuartzHelpers.DeleteTask(jobKey);
            }
            else if (task.TYPE == "telnet")
            {
                var jobKey = new JobKey("job_" + task.ID, "telnetGroup");
                TaskService.taskIds.Remove(taskId);
                QuartzHelpers.DeleteTask(jobKey);
            }

            return taskId;

        }


        [HttpPost]
        public List<JobKey> showAllTask()
        {
            return QuartzHelpers.ShowAllTask().Result;
        }
    }
}